package com.smartpro.mis.rest.modular.auth.util;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class NewImageUtils {
	/**
	 * 
	 * @Title: 构造图片
	 * @Description: 生成水印并返回java.awt.image.BufferedImage
	 * @param file      源文件(图片)
	 * @param waterFile 水印文件(图片)
	 * @param x         距离右下角的X偏移量
	 * @param y         距离右下角的Y偏移量
	 * @param alpha     透明度, 选择值从0.0~1.0: 完全透明~完全不透明
	 * @return BufferedImage
	 * @throws IOException
	 */
	public static BufferedImage watermark(File file, File waterFile, int x, int y, float alpha) throws IOException {
		// 获取底图
		BufferedImage buffImg = ImageIO.read(file);
		// 获取层图
		BufferedImage waterImg = ImageIO.read(waterFile);
		// 创建Graphics2D对象，用在底图对象上绘图
		Graphics2D g2d = buffImg.createGraphics();
		int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
		int waterImgHeight = waterImg.getHeight();// 获取层图的高度
		// 在图形和图像中实现混合和透明效果
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
		// 绘制
		g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
		g2d.dispose();// 释放图形上下文使用的系统资源
		return buffImg;
	}

	public static BufferedImage watermark(BufferedImage buffImg, BufferedImage waterImg, int x, int y, float alpha)
			throws IOException {
		// 获取底图
		// BufferedImage buffImg = ImageIO.read(file);
		// 获取层图
		// BufferedImage waterImg = ImageIO.read(waterFile);
		// 创建Graphics2D对象，用在底图对象上绘图
		Graphics2D g2d = buffImg.createGraphics();
		int waterImgWidth = waterImg.getWidth();// 获取层图的宽度
		int waterImgHeight = waterImg.getHeight();// 获取层图的高度
		// 在图形和图像中实现混合和透明效果
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
		// 绘制
		g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
		g2d.dispose();// 释放图形上下文使用的系统资源
		return buffImg;
	}

	/**
	 * 输出水印图片
	 * 
	 * @param buffImg  图像加水印之后的BufferedImage对象
	 * @param savePath 图像加水印之后的保存路径
	 */
	public static void generateWaterFile(BufferedImage buffImg, String savePath) {
		int temp = savePath.lastIndexOf(".") + 1;
		try {
			ImageIO.write(buffImg, savePath.substring(temp), new File(savePath));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public static String getNewImage(String imgDataStr, byte[] qrCode) throws IOException {
		String ret="";
		String[] str1 = imgDataStr.split(",");
		String imgData = "";
		if (str1.length > 1)
			imgData = str1[1];
		else
			imgData = imgDataStr;
		BASE64Decoder decoder = new BASE64Decoder();

		// Base64解码
		byte[] imgByte = decoder.decodeBuffer(imgData);
		for (int i = 0; i < imgByte.length; ++i) {
			if (imgByte[i] < 0) {// 调整异常数据
				imgByte[i] += 256;
			}
		}
		ByteArrayInputStream buffin = null;
		InputStream qrCodebuffin = null;
		try {
			buffin = new ByteArrayInputStream(imgByte);
			BufferedImage couponImage = ImageIO.read(buffin);
			// 商品主图
			// String imageUrl = "https:" + taoBaoQRCode.getPictUrl();
			// URL url = new URL(imageUrl);
			qrCodebuffin = new ByteArrayInputStream(qrCode);
			BufferedImage picImage = ImageIO.read(qrCodebuffin);
			BufferedImage modifyImage = NewImageUtils.watermark(couponImage, picImage, 0, 0, 1.0f);
			ret=NewImageUtils.encodeToString(modifyImage);
			//NewImageUtils.generateWaterFile(modifyImage, "D://img//new.png");
		} catch (Exception e) {
			// LOGGER.error("getTaoBaoqQRCode error");
			e.printStackTrace();
		} finally {
			buffin.close();
			qrCodebuffin.close();
			// os.close();
		}
		return ret;
	}
	/**
	 * 将图片转换成base64格式进行存储
	 * @param imagePath
	 * @return
	 */
	public static String encodeToString(BufferedImage image) throws IOException {
	    String imageString = null;
	    ByteArrayOutputStream bos = new ByteArrayOutputStream();
	    try {
	        ImageIO.write(image, "png", bos);
	        byte[] imageBytes = bos.toByteArray();
	        BASE64Encoder encoder = new BASE64Encoder();
	        imageString = encoder.encode(imageBytes);
	        bos.close();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	    return imageString;
	}
	/**
	 * 
	 * @param args
	 * @throws IOException IO异常直接抛出了
	 * @author bls
	 */
	public static void main(String[] args) throws IOException {
		String sourceFilePath = "D://img//di.jpg";
		String waterFilePath = "D://img//ceng.png";
		String saveFilePath = "D://img//new.png";
		NewImageUtils newImageUtils = new NewImageUtils();
		// 构建叠加层
		BufferedImage buffImg = NewImageUtils.watermark(new File(sourceFilePath), new File(waterFilePath), 0, 0, 1.0f);
		// 输出水印图片
		newImageUtils.generateWaterFile(buffImg, saveFilePath);
	}
}